package ems.dev.easycode.util;

import ems.dev.easycode.common.FreemarkerEntity;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/5/25
 * @function 封装freemarker通用API
 */
public class FreemarkerUtil {

    /**
     * 基于freemarker配置项，生成最终文件
     */
    public static void generateFinalFile(FreemarkerEntity config){
        Configuration configuration = Configuration.getDefaultConfiguration();
        configuration.setClassForTemplateLoading(FreemarkerUtil.class,config.getTemplateDir());
        Template template = null;
        try {
            template = configuration.getTemplate(config.getTemplateName());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            template.process(config.getDataSource(),new OutputStreamWriter(new FileOutputStream(config.getOutputPath()+"\\"+config.getOutputName()),"utf-8"));
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
